/*
 * FORM CONTROL
 * - Description: 
 * 		+ Use to add and remove answer input box
 *  	+ Limit checkbox selected
 * - Create date: 27-6-2015 - Author: Tran Quoc Toan (toanlove371@gmail.com)
 * - Last edited: 27-6-2015 by Tran Quoc Toan (toanlovle371@gmail.com) 
 */


var indexRoot = 1;		//number of answers have existed in the list
var index;				//the loop index, use to name the answer added

/**
 * initialize the index, base on the indexRoot
 */
function initIndex() {
	index = indexRoot - 1;
}

/**
 * add new answer input box, name it to put value through ModelAttribute
 */
function addRow() {
	var answerList = document.getElementById('list-answer');
    var div = document.createElement('div');

    div.className = 'answer-row';
    index++;
    div.innerHTML = '<input name=\"answerList['+index+'].answerContent\" id="answerList'+index+'.answerContent" type="text" value="" class="form-control" required="required" placeholder="Đáp án '+(index+1)+'"></input>\
        \<br> ';

    answerList.appendChild(div);
}

/**
 * remove the answer input box
 */
function removeRow() {
	if (index > indexRoot - 1) {
		var answerList = document.getElementById('list-answer');
		var row = document.getElementById('answerList'+index+'.answerContent');
		answerList.removeChild( row.parentNode );
		index--;
	} else {
		alert('Phải có ít nhất 2 câu trả lời');
	}
}

//=============================================================

/***********************************************
* Limit number of checked checkboxes script- by JavaScript Kit (www.javascriptkit.com)
* This notice must stay intact for usage
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and 100s more
***********************************************/

function checkboxlimit(checkgroup, limit){
	if (limit > 0) {
		var checkgroup=checkgroup;
		var limit=limit;
		for (var i=0; i<checkgroup.length; i++){
			checkgroup[i].onclick=function(){
			var checkedcount=0;
			for (var i=0; i<checkgroup.length; i++)
				checkedcount+=(checkgroup[i].checked)? 1 : 0
			if (checkedcount>limit){
				alert("Bạn chỉ được chọn tối đa "+limit+" đáp án")
				this.checked=false;
				}
			}
		}
	}
}
